package chapter08

/**
  * create by undeRdoG on  2021-06-06  13:16
  * 凡心所向，素履以往，生如逆旅，一苇以航。
  */
object Test01_PatternMatchBase {

  def main(args: Array[String]): Unit = {
    val x: Int = 2
    val y: String = x match {
      case 1 => "one"
      case 2 => "two"
      case 3 => "three"
      case _ => "other"
    }
    println(y)

    //  用模式匹配实现二元运算
    val a = 25
    val b = 13

    def matchDualOp(op: Char): Any = op match {
      case '+' => a + b
      case '-' => a - b
      case '*' => a * b
      case '/' => a / b
      case _ => "exception"
    }

    println(matchDualOp('*'))

    //  模式守卫  求一个数的绝对值

    def abs(num: Int): Int = {
      num match {
        case i if i >= 0 => num
        case i if i < 0 => -num
      }
    }

    println(abs(-83))


  }
}
